package com.javajun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.custom.DataGridParam;
import com.javajun.custom.EasyuiUtils;
import com.javajun.custom.Success;
import com.javajun.entity.Teacher;
import com.javajun.form.StudentForm;
import com.javajun.form.TeacherForm;
import com.javajun.service.TeacherService;

@Controller
@RequestMapping(value="/teacher")
public class TeacherController {
    
    @Autowired
    private TeacherService teacherService;
    
    @Autowired
    private JavaMailSender jms;
    
    @RequestMapping(value="/manage")
    public String manageView() {
        return "teacher/manage";
    }
    
    @RequestMapping(value="/search")
    public String searchView(TeacherForm form, ModelMap map) {
        map.put("model", form);
        return "teacher/search";
    }
    
    @RequestMapping(value="/edit")
    public String editView(Integer id, ModelMap map) {
        Teacher model = new Teacher();
        if(id != null) {
            model = teacherService.findById(id);
        }
        map.put("model", model);
        return "teacher/edit";
    }
    
    @RequestMapping(value="/list.do", method=RequestMethod.POST)
    @ResponseBody
    public Object listDo(DataGridParam param, TeacherForm form) {
        Pageable pageable = param.buildPageable();
        Specification<Teacher> spec = teacherService.buildSpec(form);
        Page<Teacher> page = teacherService.findAll(spec, pageable);
        return EasyuiUtils.buildDatagridResult(page);
    }
    
    @RequestMapping(value="/save", method=RequestMethod.POST)
    @ResponseBody
    public Object saveDo(StudentForm form) {
        Success success;
        try {
            teacherService.save(form);
            success = new Success(true, "保存数据成功");
            //建立邮件消息
            SimpleMailMessage mainMessage = new SimpleMailMessage();
            //发送者
            mainMessage.setFrom("admin@javajun.com");
            //接收者
            mainMessage.setTo(form.getEmail());
            //发送的标题
            mainMessage.setSubject("注册成功");
            //发送的内容
            mainMessage.setText("您好，管理员已成功为您在图书管理系统中注册！用户名为您的邮箱~默认密码为：1111，请您即时登录并修改密码！");
            try {
                jms.send(mainMessage);
            } catch (Exception e) {
                e.printStackTrace();
                success = new Success(true, "数据保存成功，但给用户发送邮件失败哦~");
            }
        } catch (Exception e) {
            success = new Success(false, "出现了不可预料的错误，可能是您的邮箱已被注册~~");
        }
        return success;
    }
    
    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ResponseBody
    public Object deleteDo(Integer id) {
        Success success;
        try {
            teacherService.deleteById(id);
            success = new Success(true, "删除数据成功");
        } catch (Exception e) {
            // e.printStackTrace();
            success = new Success(false, "由于不知道什么原因，可能是您没选中，反正您删除失败了~~~");
        }
        return success;
    }
}
